<template>
	<view class="menu">
		<view class="menu_nav row">
			<p :class="navIndex==0?'active':''" @click.stop="handleChoose(0)">On Shelf</p>
			<p :class="navIndex==1?'active':''" @click.stop="handleChoose(1)">Off Shelf</p>
		</view>
		<ul>
			<li v-for="(item,index) in menuList" :key="index" @click="handleDetails(item)">
				<dl>
					<dt class="row" v-if="item.imgurls">
						<image :src="item.imgurls.split(',')[0]" mode="aspectFit"></image>
					</dt>
					<dd>
						<view class="menu_top row">
							<p>{{item.name}}</p>
							<view class="menu_icon row">
								<span @click.stop="handlDelete(item)"><u-icon name="trash" color="#666" size="18"></u-icon></span>
								<span @click.stop="handlEdit(item.itemid)"><u-icon name="edit-pen" color="#666" size="18" ></u-icon></span>
							</view>
						</view>
						<view class="menu_gray">
							{{item.introduce}}
						</view>
						<view class="menu_money row">
							<text>{{item.discount_price||'0.00'}}Egp</text>
							<font>-{{item.discount}}%</font>
							<span>{{item.origin_price}}Egp</span>
						</view>
						<p>Spend {{item.reach}}Egp, get {{item.minus}}Egp off</p>
						<view class="menu_btn">
							<button @click.stop="handleState(item)">{{state==0?'On Shelf':"Off Shelf"}}</button>
						</view>
					</dd>
				</dl>
			</li>
		</ul>
		<view class="footer">
			<button @click.stop="handleAdd">Create product</button>
		</view>
	</view>
</template>

<script>
	import {
		itemListForShoper,
		upItem,
		downItem,
		deleteItem
	} from '@/lib/service.js'
	export default {
		data() {
			return {
				navIndex: 0,
				state: 1,
				loadStatus: 'loadmore',
				currentPage: 1,
				pageSize: 10,
				hasMore: false,
				menuList: []
			};
		},
		onLoad() {
			this.loadData();
		},
		onReachBottom() {
			if (this.hasMore) {
				this.currentPage = this.currentPage + 1;
				this.loadData();
			}
		},
		methods: {
			handleChoose(index) {
				this.navIndex = index;
				if (index == 1) {
					this.state = 0;
				} else {
					this.state = 1;
				}
				this.currentPage = 1;
				this.menuList = [];
				this.loadData(true);
			},
			loadData(isReset) {
				this.loadStatus = 'loading';
				let obj = {
					pageNo: this.currentPage,
					pageSize: this.pageSize,
					state: this.state,
				}
				itemListForShoper(obj).then((res) => {
						uni.hideLoading();
						this.loadStatus = 'no-more';
						this.hasMore = res.data.hasMore;
						if (isReset) {
							this.menuList = res.data.records;
						} else {
							this.menuList = this.menuList.concat(res.data.records);
						}
					})
					.catch((res) => {
						this.loadStatus = 'no-more';
						uni.hideLoading();
						// showFailure();
					});
			},
			handleAdd() {
				uni.navigateTo({
					url: './goods'
				})
			},
			handlEdit(itemid){
				uni.navigateTo({
					url: './goods?itemid='+itemid
				})
			},
			handleDetails(item){
				uni.navigateTo({
					url: './goods_details?itemid='+item.itemid
				})
			},
			handleState(item) {
				let _this = this;
				let stateName = item.state == 0 ? 'On Shelf' : 'Off Shelf';
				uni.showModal({
					title: 'prompt',
					content: `Are you sure you want to${stateName}This product？`,
					success: function(res) {
						if (res.confirm) {
							_this.handleItem(item)
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			handleItem(item) {
				let _this = this;
				if (item.state == 0) {
					upItem({
						itemid: item.itemid
					}).then((res) => {
						uni.showToast({
							icon: 'none',
							title: 'On Shelf successfully',
						})
						_this.currentPage = 1;
						_this.menuList = [];
						_this.loadData(true);
					})
				} else {
					downItem({
						itemid: item.itemid
					}).then((res) => {
						uni.showToast({
							icon: 'none',
							title: 'Off Shelf successfully',
						})
						_this.currentPage = 1;
						_this.menuList = [];
						_this.loadData(true);
					})
				}
			},
			// 删除商品
			handlDelete(item) {
				let _this = this;
				uni.showModal({
					title: 'prompt',
					content: `Are you sure you want to delete this product？`,
					success: function(res) {
						if (res.confirm) {
							deleteItem({
								itemid: item.itemid
							}).then((res) => {
								uni.showToast({
									icon: 'none',
									title: 'Delete successfully',
								})
								_this.currentPage = 1;
								_this.menuList = [];
								_this.loadData(true);
							})
						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});

			}
		}
	}
</script>

<style lang="scss" scoped>
	.menu {
		padding: 36rpx 28rpx;
		padding-bottom: 160rpx;

		.menu_nav {
			justify-content: space-between;

			.active {
				border: 2rpx solid #fc721a !important;
				color: #fc721a !important;
			}

			p {
				width: 332rpx;
				height: 80rpx;
				border: 2rpx solid #eaeaea;
				border-radius: 22.9rpx;
				line-height: 80rpx;
				font-weight: 500;
				color: #333333;
				font-size: 30rpx;
				text-align: center;
			}
		}

		ul {
			li {
				margin-top: 40rpx;

				dl {
					display: flex;

					dt {
						image {
							width: 230rpx;
							height: 221rpx;
							border-radius: 20rpx;
							background: #eeeeee;
						}
					}

					dd {
						width: 70%;
						padding-left: 16rpx;

						.menu_top {
							justify-content: space-between;

							p {
								font-weight: 500;
								color: #333333;
								font-size: 28rpx;
							}
						}

						.menu_gray {
							margin: 10rpx 0;
							background: #f5f5f5;
							border-radius: 10rpx;
							padding: 4rpx 12rpx;
							font-family: PingFang SC;
							color: #666666;
							font-size: 22rpx;
							display: inline-block;
						}

						.menu_money {
							text {
								font-family: PingFang SC;
								font-weight: 600;
								color: #f39a4b;
								font-size: 28rpx;
							}

							font {
								width: 50rpx;
								height: 23rpx;
								background: rgba(252, 114, 26, 0.06);
								border-radius: 23rpx;
								line-height: 23rpx;
								color: #f39a4b;
								font-size: 18rpx;
								text-align: center;
								margin: 0 12rpx;
								margin-top: 6rpx;
							}

							span {
								width: 43.89rpx;
								height: 30.53rpx;
								line-height: normal;
								font-family: PingFang SC;
								color: rgb(40, 215, 150);
								font-size: 24rpx;
								text-decoration: line-through
							}
						}

						>p {
							font-family: PingFang SC;
							color: #fc1a1a;
							font-size: 24rpx;
						}

						.menu_btn {
							margin-top: 20rpx;

							button {
								width: 120rpx;
								height: 46rpx;
								background: #fc721a;
								color: #fff;
								border-radius: 22rpx;
								line-height: 46rpx;
								text-align: center;
								font-size: 24rpx;
								margin: 0;
								padding: 0;
								border: none;
								outline: none;
							}
						}
					}
				}
			}
		}
	}

	.footer {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		padding: 30rpx 0;

		button {
			width: 92%;
			border-radius: 50rpx;
			height: 92rpx;
			background: #fc721a;
			line-height: 92rpx;
			font-family: Montserrat;
			font-weight: 500;
			color: #ffffff;
			font-size: 30rpx;
			text-align: center;
			padding: 0;
			margin: 0 4%;
		}
	}
</style>